package weaver.interfaces.cese.duss.ceseutil;

import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-12-14
 * @Description: 业务申请计算工具类
 * @ModifyLog:
 **/

public class CostUtil {
    //获取业务申请单requestid
    public static String getYwsqd2Requestid(String id) {
        String dylc = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select dylc from uf_ywsqd where id=?", id );
        if (rs.next()) {
            dylc = Util.null2String( rs.getString( "dylc" ) );
        }
        return dylc;
    }

    //获取差旅申请requestid
    public static String getClsqdRequestid(String requestid) {
        String ywsqd = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select ywsqd from  formtable_main_416 where requestid=?", requestid );
        if (rs.next()) {
            ywsqd = Util.null2String( rs.getString( "ywsqd" ) );
        }
        return ywsqd;
    }

    //判断流程是否归档
    public static String getCurrentNodeType(String workflowid, String requestid) {
        //0：创建 1：审批 2：提交 3：归档
        String curentNodeType = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select currentnodetype from workflow_requestbase where workflowid=? and requestid=?", workflowid, requestid );
        if (rs.next()) {
            curentNodeType = Util.null2String( rs.getString( 1 ) );
        }
        return curentNodeType;
    }

    //判断流程是否存在
    public static boolean isWorkflowExist(String requestid) {
        RecordSet rs = new RecordSet();
        boolean flag = false;
        rs.executeQuery( "select * from uf_yszxxxb where lc='" + requestid + "'" );
        if (rs.next()) {
            flag = true;
        }
        return flag;
    }

    //判断是否存在流程,存在则更新,不存在则插入
    public static boolean deleteYszxxxb(String requestid) {
        RecordSet rs = new RecordSet();
        boolean flag = false;
        rs.writeLog("delete uf_yszxxxb where lc=?", requestid );
        if (rs.executeUpdate( "delete uf_yszxxxb where lc=?", requestid )) {
            flag = true;
        }
        return flag;
    }

    //判断是否存在流程,存在则更新,不存在则插入
    public static boolean insertYszxxxb(String requestid, String yshd, String yskm, String yszt, String ysqj, String ysje, String yslx, String sfrq) {
        RecordSet rs = new RecordSet();
        boolean flag = false;
        String formmodeid = getFormmodeId( "uf_yszxxxb" );
        String sql = "insert into uf_yszxxxb(lc,yshd,yskm,yszt,ysqj,ysje,yslx,sfrq,formmodeid) " +
                "values (?,?,?,?,?,?,?,?,?)";
        Object[] params = new Object[]{requestid, yshd, yskm, yszt, ysqj, ysje, yslx, sfrq, formmodeid};
        if (rs.executeUpdate( sql, params )) {
            flag = true;
        }
        return flag;
    }

    //根据当前日期判断所属季度最后一天
    public static String getDate2JdEnd(String date) {
        String year = date.substring( 0, 4 );
        String month = date.substring( 5, 7 );
        String jd1 = "'01','02','03'";
        String jd2 = "'04','05','06'";
        String jd3 = "'07','08','09'";
        String jd4 = "'10','11','12'";
        if (jd1.contains( month )) {
            return year + "-03-31";
        } else if (jd2.contains( month )) {
            return year + "-06-30";
        } else if (jd3.contains( month )) {
            return year + "-09-30";
        } else if (jd4.contains( month )) {
            return year + "-12-31";
        } else {
            return "";
        }
    }

    //1、根据(yshd、yskm、yszt、rq)预算执行信息获取可用金额
    public static double getYszxxKyys(String yshd, String fykm, String yszt, String sqrq) {
        String rq = getDate2JdEnd( sqrq );
        RecordSet rs = new RecordSet();
        rs.writeLog( "----------getYszxxKyys------------" );
        String sql = "select nvl(sum(nvl(jdcb,0)),0) from uf_yszxxx  where yshdbh =? and yszt=? " +
                "and rq <=? and sjkm=(select sjkm from uf_yskmxx where id=?)";
        rs.executeQuery( sql, yshd,yszt,rq,fykm );
        rs.writeLog("sql="+sql+","+yshd+","+yszt+","+rq+","+fykm);
        rs.next();
        double jdcbkyys = Util.getDoubleValue( rs.getString( 1 ) );
        return jdcbkyys;
    }

    //1、预算执行信息表（根据lc、yshd、yskm、yszt、djrq、yslx)获取占用、发生、释放的预算金额
    public static double getYszxxxbYsje(String lc, String yshd, String yskm, String yszt, String djrq, String yslx) {
        RecordSet rs = new RecordSet();
        rs.writeLog( "----------getYszxxxbYsje------------" );
        String sql = "select nvl(sum(nvl(ysje,0)),0) from uf_yszxxxb where yskm in (select t1.id from uf_yskmxx t1 " +
                "where t1.sjkm=(select t2.sjkm from uf_yskmxx t2 where t2.id=?)) \n" +
                "and lc !=? and yshd = ? and yszt=? and yslx in ("+yslx+") and ysqj <=?";
        rs.executeQuery( sql, yskm, lc, yshd, yszt, djrq );
        rs.writeLog("sql="+sql+","+yskm+","+lc+","+yshd+","+yszt+","+djrq);
        rs.next();
        double ysje = Util.getDoubleValue( rs.getString( 1 ) );
        return ysje;
    }
    //获取formmodeid
    public static String getFormmodeId(String tableName) {
        String formmodeid = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?", tableName );
        rs.next();
        formmodeid = Util.null2String( rs.getString( "id" ) );
        return formmodeid;
    }

    //根据费用科目id获取当前费用科目名称
    public static String getFykmId2Name(String id) {
        RecordSet rs = new RecordSet();
        String ysfykm = "";
        rs.executeQuery( "select ysfykm from  uf_yskmxx where id=? and sfzy='0'", id );
        if (rs.next()) {
            ysfykm = Util.null2String( rs.getString( "ysfykm" ) );
        }
        return ysfykm;
    }

}
